package chatclient_01.dialogs;

import chatclient_01.ChatClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

public class ConnectDialog extends JDialog {

  private static final long serialVersionUID = 1L;
  private ChatClient chatClient;
  private JTextField hostTextField;
  private JButton jButton1;
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JTextField nameTextField;

  public ConnectDialog(ChatClient parent, boolean modal) {
    super(parent, modal);
    initComponents();
    this.chatClient = parent;

    this.hostTextField.selectAll();
  }

  private void initComponents() {
    this.hostTextField = new JTextField();
    this.nameTextField = new JTextField();
    this.jLabel1 = new JLabel();
    this.jLabel2 = new JLabel();
    this.jButton1 = new JButton();

    setTitle("Connect");
    setLocationByPlatform(true);
    setResizable(false);

    this.hostTextField.setText("localhost");
    this.hostTextField.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        ConnectDialog.this.hostTextFieldActionPerformed(evt);
      }
    });
    this.nameTextField.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        ConnectDialog.this.nameTextFieldActionPerformed(evt);
      }
    });
    this.jLabel1.setText("Host:");

    this.jLabel2.setText("Name:");

    this.jButton1.setText("OK");
    this.jButton1.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {
        ConnectDialog.this.jButton1ActionPerformed(evt);
      }
    });
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false).addGroup(layout.createSequentialGroup().addComponent(this.jLabel1).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.hostTextField, -2, 117, -2)).addGroup(layout.createSequentialGroup().addComponent(this.jLabel2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.nameTextField))).addContainerGap(-1, 32767)).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(95, 32767).addComponent(this.jButton1, -2, 68, -2).addContainerGap()));

    layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel1).addComponent(this.hostTextField, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.nameTextField, -2, -1, -2).addComponent(this.jLabel2)).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.jButton1).addContainerGap(-1, 32767)));

    pack();
  }

  private void jButton1ActionPerformed(ActionEvent evt) {
    if ((this.hostTextField.getText().length() == 0) || (this.nameTextField.getText().length() == 0)) {
      JOptionPane.showMessageDialog(this, "Fields shouldn't be null.", "Error", 0, null);
    }
    this.chatClient.connectToHost(this.hostTextField.getText().trim(), this.nameTextField.getText().trim());
    setVisible(false);
  }

  private void nameTextFieldActionPerformed(ActionEvent evt) {
    if ((this.hostTextField.getText().length() == 0) || (this.nameTextField.getText().length() == 0)) {
      JOptionPane.showMessageDialog(this, "Fields shouldn't be null.", "Error", 0, null);
    } else {
      this.jButton1.doClick();
    }
  }

  private void hostTextFieldActionPerformed(ActionEvent evt) {
    if ((this.hostTextField.getText().length() == 0) || (this.nameTextField.getText().length() == 0)) {
      JOptionPane.showMessageDialog(this, "Fields shouldn't be null.", "Error", 0, null);
    } else {
      this.jButton1.doClick();
    }
  }
}